package com.project.nimsen.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;

import com.project.nimsen.common.FileUpload;
import com.project.nimsen.common.impl.FileUploadWebInsert;
import com.project.nimsen.common.impl.OOSUpload;
import com.project.nimsen.constant.NimsenConstant;

public class FileVisitUtil {
	//update fileinfo set filename = replace(filename,"upload","file") where active=1;--替换列中的某一字符串
	//SET SQL_SAFE_UPDATES = 0;--设置msql可以批量更新
	/**
	 * 得到上传类
	 * */
	public static FileUpload getFileUpload(MultipartFile uploadFile, HttpServletRequest request)throws Exception{
		String memorytype = NimsenUtil.getMemoryType();
		if(NimsenConstant.memory_type.oos.toString().equals(memorytype)){
			return new OOSUpload(confileByMulti(uploadFile), request);
		}
		String srcname = new String(uploadFile.getOriginalFilename().getBytes("ISO8859-1"),"UTF-8");
		return new FileUploadWebInsert(uploadFile.getBytes(), srcname, request);
	}
	//把MultipartFile转换成file
	private static File confileByMulti(MultipartFile uploadFile) throws Exception{
		File file = new File(uploadFile.getOriginalFilename());
		InputStream stream = uploadFile.getInputStream();
		FileOutputStream fs=new FileOutputStream(uploadFile.getOriginalFilename());
		byte[] buffer =new byte[1024*1024];
		int byteread = 0;
		while ((byteread=stream.read(buffer))!=-1){
			fs.write(buffer,0,byteread);
			fs.flush();
		}
		fs.close();
		stream.close();
		return file;
	}
	/**
	 * 把文件输出
	 * */
	public static void putoutFile(HttpServletRequest request, HttpServletResponse response)throws Exception{
		String url = request.getServletPath();
		String filename = url.substring(url.lastIndexOf("/")+1);
		
		File file = null;
		//根据存储类型得到文件
		String memorytype = NimsenUtil.getMemoryType();
		if(NimsenConstant.memory_type.oos.toString().equals(memorytype)){
			String insertWebUrl = NimsenUtil.getOOSInsertUrl();
			String filepath = "http://"+NimsenUtil.getOSSBucketName()+"."+insertWebUrl+"/"+filename;
			response.sendRedirect(filepath);
			
//			OSSClient client = F273cUtil.getOSSClient();
//			String buckname = F273cUtil.getOSSBucketName();
//			OSSObject obj = client.getObject(buckname, filename);
//			InputStream in = obj.getObjectContent();
//			OutputStream out = response.getOutputStream();
//			byte[] bys = new byte[1024*1024];
//			while(in.read(bys)>0){
//				out.write(bys);
//			}
		}else
			file = insertFile(filename, request);
		
		
		FileInputStream in = new FileInputStream(file);
		OutputStream out = response.getOutputStream();
		byte memory [] = new byte[100];
		int bytes;
		while((bytes=in.read(memory))!=-1){
			out.write(memory);
		}
	}
	
	/**
	 * 网站内部文件
	 * */
	private static File insertFile(String filename, HttpServletRequest request)throws Exception{
		ServletContext sevcon = request.getSession().getServletContext();
		String allPath = sevcon.getRealPath(File.separator+NimsenConstant.UPLOAD_PATH);
		return new File(allPath+File.separator+filename);
	}
}
